//package com.shanghaionstar.mro.util;
//
//import android.app.Activity;
//import android.content.Context;
//import android.os.Handler;
//import android.os.Message;
//
//import com.onstar.cn.auth.userProfile.FeatureEnum;
//import com.onstar.cn.common.exception.HttpException;
//import com.onstar.cn.common.util.Constants;
//import com.onstar.cn.mag.enumtype.RemoteCommandsType;
//import com.onstar.cn.vehicle.entity.ErrorInfo;
//import com.onstar.cn.vehicle.entity.RequestType;
//import com.shanghaionstar.R;
//import com.shanghaionstar.discover.charge.RemoteCommandUtil;
//import com.shanghaionstar.enumtype.FunctionNameFindId;
//import com.shanghaionstar.mro.MrOActivity;
//import com.shanghaionstar.pin.PinDialogFragment;
//import com.shanghaionstar.pin.PinUtil;
//import com.shanghaionstar.utils.AlertDialogUtil;
//import com.shanghaionstar.utils.BusinessUtil;
//
//import java.util.HashMap;
//import java.util.Map;
//
//public class MrORemoteControlUtil implements PinDialogFragment.PinResultListener {
//
//    private Activity activity;
//    private String request;
//    private MrOActivity.OnMroOperationCallbackListener operationCallbackListener;
//
//    @Override
//    public void onPinSuccess(String commandsType, boolean isFlash, boolean isHorn) {
//        RemoteCommandsType remoteCommandsType = RemoteCommandsType.fromValue(commandsType);
//        operationCallbackListener.onRemoteControlCallbackListener(false, String.format(activity.getString(R.string.UC009_MSG006), getRequestStringByType(remoteCommandsType)));
//        noPinRemoteControl(mHandler, remoteCommandsType, request, activity);
//    }
//
//    @Override
//    public void onPinFail() {
//        ErrorInfo errorInfo = new ErrorInfo();
//        errorInfo.setCode("cancel");
//        errorInfo.setMsg("pin input canceled");
//        /**Couldn't happen**/
//        operationCallbackListener.onRemoteControlCallbackListener(false, "Cancel input pin");
//    }
//
//    @Override
//    public void onPinCancel() {
//
//    }
//
//    private static class MrORemoteControlUtilInner {
//        static MrORemoteControlUtil requestUtil = new MrORemoteControlUtil();
//    }
//
//    private MrORemoteControlUtil() {
//    }
//
//    public final static MrORemoteControlUtil getInstance() {
//        return MrORemoteControlUtilInner.requestUtil;
//    }
//
//    public void remoteControl(String request, MrOActivity.OnMroOperationCallbackListener operationCallbackListener, Activity activity) {
////        if (!BusinessUtil.isServiceEntitlement(activity, Constants.ENTITLEMENT_M07REMOTE_CONTROL)) {
////            operationCallbackListener.onRemoteControlCallbackListener(true, activity.getString(R.string.UC009_MSG007));
////            return;
////        }
//
//        /**
//         * 需要弹出框提示用户买包
//         */
//        if(!BusinessUtil.checkPackageRight(Constants.ENTITLEMENT_M07REMOTE_CONTROL, activity,true)) {
//            operationCallbackListener.onRemoteControlCallbackListener(true, BusinessUtil.getCheckPackageRightString(Constants.ENTITLEMENT_M07REMOTE_CONTROL,activity));
//            return;
//        }
//        RemoteCommandsType remoteCommandsType = null;
//        RequestType requestType = null;
//        FeatureEnum featureEnum = null;
//        if ("LockDoors".equals(request)) {
//            requestType = RequestType.LockDoorsRequest;
//            remoteCommandsType = RemoteCommandsType.COMMANDS_LOCK_DOOR;
//            featureEnum = FeatureEnum.LOCKDOOR;
//        } else if ("UnlockDoors".equals(request)) {
//            requestType = RequestType.UnlockDoorsRequest;
//            remoteCommandsType = RemoteCommandsType.COMMANDS_UNLOCK_DOOR;
//            featureEnum = FeatureEnum.UNLOCKDOOR;
//        } else if ("RemoteStart".equals(request)) {
//            requestType = RequestType.RemoteStartRequest;
//            remoteCommandsType = RemoteCommandsType.COMMANDS_START;
//            featureEnum = FeatureEnum.START;
//        } else if ("CancelStart".equals(request)) {
//            requestType = RequestType.RemoteStopRequest;
//            remoteCommandsType = RemoteCommandsType.COMMANDS_CANCEL_START;
//            featureEnum = FeatureEnum.CANCELSTART;
//        } else if ("FlashLights".equals(request) ||
//                "Horn".equals(request) ||
//                "FlashHorn".equals(request)) {
//            requestType = RequestType.VehicleAlertRequest;
//            remoteCommandsType = RemoteCommandsType.COMMANDS_ALERT;
//            featureEnum = FeatureEnum.ALERT;
//        } else {
//            /**Couldn't happen**/
//            operationCallbackListener.onRemoteControlCallbackListener(true, "Unknown request" + request);
//            return;
//        }
//        if (!BusinessUtil.isServiceFeature(featureEnum.value(), activity)) {
//            operationCallbackListener.onRemoteControlCallbackListener(true, activity.getString(R.string.UC009_MSG008));
//            return;
//        }
//        if (BusinessUtil.checkInProgressCommand(activity, null, false)) {
//            operationCallbackListener.onRemoteControlCallbackListener(true, activity.getString(R.string.UC009_MSG005));
//            return;
//        }
//        this.activity = activity;
//        this.request = request;
//        this.operationCallbackListener = operationCallbackListener;
//        if (PinUtil.isNeedPin(activity)) {
////            MrOPinFragment pin = new MrOPinFragment();
//            PinDialogFragment pinDialogFragment = PinDialogFragment.newInstance(remoteCommandsType.value());
//            pinDialogFragment.setPinResultListener(this);
//            operationCallbackListener.onRemoteControlCallbackListener(pinDialogFragment);
////            MrOActivity mrOActivity = (MrOActivity)activity;
////            pinDialogFragment.show(mrOActivity.getSupportFragmentManager(), PinDialogFragment.class.getSimpleName());
//        } else {
//            operationCallbackListener.onRemoteControlCallbackListener(false, String.format(activity.getString(R.string.UC009_MSG006), getRequestStringByType(remoteCommandsType)));
//            noPinRemoteControl(mHandler, remoteCommandsType, request, activity);
//        }
//    }
//
//    Handler mHandler = new Handler() {
//        public void handleMessage(Message msg) {
//            ErrorInfo errorInfo;
//            RemoteCommandsType commandType;
//            switch (msg.what) {
//                case Constants.SERVICE_RESPONSE_FAILURE:
//                    errorInfo = (ErrorInfo) msg.getData().getSerializable("errorInfo");
//                    commandType = (RemoteCommandsType) msg.getData().getSerializable("commandType");
//                    AlertDialogUtil.showResultAlertDialog(activity, false, commandType, errorInfo);
//                    break;
//                case Constants.SERVICE_RESPONSE_INPROGRESS:
////                    commandType = (RemoteCommandsType) msg.getData().getSerializable("commandType");
////                    AlertDialogUtil.showAlertDialog(activity, activity.getString(R.string.remote_control),
////                            RemoteCommandUtil.getRequestStringByType(activity, commandType) + activity.getString(R.string.remote_waiting));
//                    break;
//                case Constants.SERVICE_HTTP_ERROR:
//                    HttpException e = (HttpException) msg.getData().getSerializable("HttpException");
//                    AlertDialogUtil.showDialogByExceptionCode(activity, e);
//                    break;
//                default:
//                    break;
//            }
//        }
//    };
//
//    /**
//     * 不需要pin码 直接请求
//     */
//    private void noPinRemoteControl(final Handler handler, final RemoteCommandsType commandType, String alertType, Context context) {
//        RemoteCommandUtil remoteCommandUtil = new RemoteCommandUtil(context, handler);
//        if (commandType == RemoteCommandsType.COMMANDS_ALERT) {
//            Map<String, Boolean> alertParam = new HashMap<String, Boolean>();
//            remoteCommandUtil.setAlertParam(alertParam);
//            if ("FlashLights".equals(alertType)) {
//                alertParam.put("Flash", true);
//                alertParam.put("Honk", false);
//            } else if ("Horn".equals(alertType)) {
//                alertParam.put("Flash", false);
//                alertParam.put("Honk", true);
//            } else if ("FlashHorn".equals(alertType)) {
//                alertParam.put("Flash", true);
//                alertParam.put("Honk", true);
//            }
//            remoteCommandUtil.setAlertParam(alertParam);
//        }
//        remoteCommandUtil.command(commandType);
//    }
//
//    private void processReport(RequestType requestType, Context context) {
//        BusinessUtil.sendReportByClickType(context, FunctionNameFindId.UnlockButton.getStringValue());
//    }
//
//    private String getRequestStringByType(RemoteCommandsType requestType) {
//        String str = "";
//        switch (requestType) {
//            case COMMANDS_LOCK_DOOR:
//                str = activity.getResources().getString(R.string.lock_hint);
//                break;
//            case COMMANDS_UNLOCK_DOOR:
//                str = activity.getResources().getString(R.string.unlock_hint);
//                break;
//            case COMMANDS_START:
//                str = activity.getResources().getString(R.string.start_hint);
//                break;
//            case COMMANDS_CANCEL_START:
//                str = activity.getResources().getString(R.string.stop_hint);
//                break;
//            case COMMANDS_ALERT:
//                str = activity.getResources().getString(R.string.alert_hint);
//                break;
//            default:
//                break;
//        }
//        return str;
//    }
//}
